﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;

namespace VISUALFINDR
{
    public class HistoryController
    {
        public static byte[] ConvertWriteableBitmapToBytes(WriteableBitmap bitmap)
        {
            long matrixSize = bitmap.PixelWidth * bitmap.PixelHeight;

            long byteSize = matrixSize * 4 + 4;

            byte[] retVal = new byte[byteSize];

            long bufferPos = 0;

            retVal[bufferPos++] = (byte)((bitmap.PixelWidth / 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelWidth % 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelHeight / 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelHeight % 256) & 0xff);

            for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
            {
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 24) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 16) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 8) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos]) & 0xff);
            }

            return retVal;
        }

        public static void SaveToDisk(byte[] buffer)
        {
            SaveToDisk(buffer, "latest.jpg");
        }

        public static void SaveToDisk(WriteableBitmap bitmap)
        {
            //bitmap.PixelHeight bitmap.PixelWidth
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream("latest.jpg", FileMode.Create, iso);
            //Extensions.SaveJpeg(bitmap, stream, 480, 300, 0, 85); // compression too high, did not work that well with microsoft logo
            Extensions.SaveJpeg(bitmap, stream, 480, 360, 0, 100);
            stream.Close();
        }

        // TODO: Crashes in big images
        public static void SaveToDisk(Stream imageStream)
        {
            byte[] InputFileData = new byte[imageStream.Length];
            imageStream.Read(InputFileData, 0, System.Convert.ToInt32(imageStream.Length));
            SaveToDisk(InputFileData);
        }

        public static byte[] LoadIfExists()
        {
            return LoadIfExists("latest.jpg");
        }

        public static void SaveToDisk(byte[] buffer, string fileName)
        {
            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Create,iso))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }
        }

        public static byte[] LoadIfExists(string fileName)
        {
            byte[] retVal = null;

            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.FileExists(fileName))
                {
                    using (IsolatedStorageFileStream stream = iso.OpenFile(fileName, FileMode.Open))
                    {
                        retVal = new byte[stream.Length];
                        stream.Read(retVal, 0, retVal.Length);
                    }
                }
                else
                {
                    retVal = new byte[0];
                }
            }
            return retVal;
        }

        public static void ClearHistory()
        {
            String fileName = "latest.jpg";
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
            if (iso.FileExists(fileName))
                iso.DeleteFile(fileName);
        }
    }
}
